package com.example.library.controller;

import com.example.library.entity.Category;
import com.example.library.entity.ApiResponse;
import com.example.library.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sx
 * @since 2024-12-17
 */
@RestController
public class CategoryController {
    @Autowired
    private CategoryMapper categoryMapper;

    //新增类别
    @PostMapping("/category/add")
    @ResponseBody
    public ResponseEntity<Category> addCategory(@RequestBody Category category){
        int result = categoryMapper.insert(category);
        if(result != 0){
            ApiResponse response = new ApiResponse("success", "类别添加成功", category);
            return new ResponseEntity<>(category, HttpStatus.OK);
        }
        else{
            ApiResponse response = new ApiResponse("error", "添加类别失败", null);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

//    //删除类别
//    @DeleteMapping("/category/delete/{id}")
//    public boolean deleteCategory(@PathVariable Long id){
//        return categoryService.removeById(id);
//    }
//
//    //更新类别
//    @PutMapping("/category/update")
//    public Category updateCategory(@RequestBody Category category) {
//        categoryService.updateById(category);
//        return category;
//    }

//    //通过ID查找类别
//    @GetMapping("/category/search/{id}")
//    public Category getCategoryByID(@PathVariable Long id){
//        return categoryService.getById(id);
//    }

    //查找全部类别
    @GetMapping("/Bookcategory/list")
    public List<Category> categories(){
        return categoryMapper.select();
    }

}
